<?php


/**************************

分页类 使用

$dl = new ListPage();
$dl->pageSize = 5; //每页显示多少数据
$dl->SetParameter('dopost','list'); //向 分页 url中添加其他参数
$dl->SetParameter('cateid','10'); 
$dl -> SetSql("SELECT * FROM  `ims_z_news`  "); //执行sql语句 注意 sql不要有 结尾分号
$list = $dl ->GetList();//获取列表$list
$page = $dl ->GetPage();//获取分页html代码


-------------

	$dl = new ListPage();//类 初始化 为 对象
	$dl->pageSize = 5;//每页显示N条记录
	//$dl->SetParameter('dopost','list');//向url中添加  其他参数 
	$dl->SetParameter('cateid',$cateid); 
	$dl -> SetSql($sql);//设置查询sql语句 不需要limit
	
	$list = $dl ->GetList();//sql查询得到的 二维数组 数据
	$page = $dl ->GetPage();//分页html代码

其他说明：
$this->queryTime
$this->Sql


********************/
class ListPage{

    var $pageNO;
    var $totalPage;
    var $totalResult;
    var $pageSize;
    var $getValues;
    var $Sql;
    var $queryTime;
	/**
     *  用指定的文档ID进行初始化
     *
     * @access    public
     * @param     string  $tplfile  模板文件
     * @return    string
     */
    function __construct($tplfile='')
    {

		
        $this->Sql='';
        $this->pageSize=25;
        $this->queryTime=0;
        $this->getValues=Array();
		
        $this->totalResult = 0;
        $this->totalPage = 0;
        $this->pageNO = 0;
       


    }
	/**
     *  兼容PHP4版本
     *
     * @access    private
     * @param     string  $tplfile  模板文件
     * @return    void
     */
    function ListPage($tplfile='')
    {
        $this->__construct($tplfile);
    }

    
    

    //设置SQL语句
    function SetSql($sql)
    {
        $this->Sql = $sql;
    }
	
    /**
     *  对config参数及get参数等进行预处理
     *
     * @access    public
     * @return    void
     */
    function PreLoad()
    {

		$pageno = isset($_GET['pageno']) ? $_GET['pageno'] : 1;
		$totalresult = isset( $_GET['totalresult'] ) ? $_GET['totalresult'] : 0  ;
        if(empty($pageno) || preg_match("#[^0-9]#", $pageno))
        {
            $pageno = 1;
        }
        if(empty($totalresult) || preg_match("#[^0-9]#", $totalresult))
        {
            $totalresult = 0;
        }
        $this->pageNO = $pageno;/**/
        $this->totalResult = $totalresult;/**/


        
		
        if($this->totalResult==0 or 1)
        {
            /**/
			$countQuery = preg_replace("#SELECT[ \r\n\t](.*)[ \r\n\t]FROM#is", 'SELECT COUNT(*) AS dd FROM', $this->Sql);
            $countQuery = preg_replace("#ORDER[ \r\n\t]{1,}BY(.*)#is", '', $countQuery);
			/*数据库操作 读取一条sql记录*/
			$row = $this -> dbRow($countQuery);
            if(!is_array($row)) $row['dd'] = 0;
            $this->totalResult = isset($row['dd'])? $row['dd'] : 0;
			/**/
			

            
			
            //$this->Sql .= " LIMIT 0,".$this->pageSize;
			$this->Sql .= " LIMIT ".(($this->pageNO-1) * $this->pageSize).",".$this->pageSize;
        }
        else
        {
            $this->Sql .= " LIMIT ".(($this->pageNO-1) * $this->pageSize).",".$this->pageSize;
        }
		//var_dump( $this->pageSize);
		$this->totalPage = ceil($this->totalResult / $this->pageSize);/**/
		if( $this->pageNO > $this->totalPage ){ $this->pageNO = $this->totalPage ;}
		
		
		
    }

    //设置网址的Get参数键值
    function SetParameter($key,$value)
    {
        $this->getValues[$key] = $value;
    }


    //获取当前页数据列表
    function GetList()
    {

		$this->PreLoad();
		
		$rsArray = array();
        $t1 = Exectime();
		//读取列表
        $rsArray = $this -> dbList($this->Sql);

        $this->queryTime = (Exectime() - $t1);
        return $rsArray;
    }

	
/**************************************************************/
//获取分页导航列表
    function GetPage()
    {
		$lang_pre_page = '上页';
		$lang_next_page = '下页';
		$lang_index_page = '首页';
		$lang_end_page = '末页';
		$lang_record_number = '条记录';
		$lang_page = '页';
		$lang_total = '共';
		
        $prepage = $nextpage = $geturl= $hidenform = '';
		
        $purl = $this->GetCurUrl();

		
        $prepagenum = $this->pageNO-1;
        $nextpagenum = $this->pageNO+1;
        if(!isset($atts['listsize']) || preg_match("#[^0-9]#", $atts['listsize']))
        {
            $atts['listsize'] = 5;
        }
        if(!isset($atts['listitem']))
        {
            $atts['listitem'] = "info,index,end,pre,next,pageno";
        }
        $totalpage = ceil($this->totalResult/$this->pageSize);

        //echo " {$totalpage}=={$this->totalResult}=={$this->pageSize}";
        //无结果或只有一页的情况
        if($totalpage<=1 && $this->totalResult > 0)
        {
            return "<span>{$lang_total} 1 {$lang_page}/".$this->totalResult.$lang_record_number."</span>";
        }
        if($this->totalResult == 0)
        {
            return "<span>{$lang_total} 0 {$lang_page}/".$this->totalResult.$lang_record_number."</span>";
        }
		
		
        $infos = "<span class = \"info\">{$lang_total} {$totalpage} {$lang_page}/{$this->totalResult}{$lang_record_number} </span>\n";
        if($this->totalResult!=0)
        {
            $this->getValues['totalresult'] = $this->totalResult;
        }
        if(count($this->getValues)>0)
        {
            foreach($this->getValues as $key=>$value)
            {
                $value = urlencode($value);
                $geturl .= "$key=$value"."&";
                $hidenform .= "<input type='hidden' name='$key' value='$value' />\n";
            }
        }
        $purl .= "?".$geturl;

        //获得上一页和下一页的链接
        if($this->pageNO != 1)
        {
            $prepage .= "<a class='prePage' href='".$purl."pageno=$prepagenum'>$lang_pre_page</a> \n";
            $indexpage = "<a class='indexPage' href='".$purl."pageno=1'>$lang_index_page</a> \n";
        }
        else
        {
            $indexpage = "<a class='indexPage'>"."$lang_index_page "."</a>\n";
        }
        if($this->pageNO != $totalpage && $totalpage > 1)
        {
            $nextpage.="<a class='nextPage' href='".$purl."pageno=$nextpagenum'>$lang_next_page</a> \n";
            $endpage="<a class='endPage' href='".$purl."pageno=$totalpage'>$lang_end_page</a> \n";
        }
        else
        {
            $endpage="<a class='endPage'>$lang_end_page</a> \n";
        }

        //获得数字链接
        $listdd = "";
        $total_list = $atts['listsize'] * 2 + 1;
        if($this->pageNO >= $total_list)
        {
            $j = $this->pageNO - $atts['listsize'];
            $total_list=$this->pageNO + $atts['listsize'];
            if($total_list > $totalpage)
            {
                $total_list = $totalpage;
            }
        }
        else
        {
            $j=1;
            if($total_list > $totalpage)
            {
                $total_list = $totalpage;
            }
        }
        for($j; $j<=$total_list; $j++)
        {
            $listdd .= $j==$this->pageNO ? "<a class=\"paginate_active\">$j</a>\n" : "<a href='".$purl."pageno=$j'>".$j."</a>\n";
        }

        $plist = "<!--分页开始-->\n";

        //info,index,end,pre,next,pageno,form
        if(preg_match("#info#i",$atts['listitem']))
        {
            $plist .= $infos;
        }
        if(preg_match("#index#i", $atts['listitem']))
        {
            $plist .= $indexpage;
        }
        if(preg_match("#pre#i", $atts['listitem']))
        {
            $plist .= $prepage;
        }
        if(preg_match("#pageno#i", $atts['listitem']))
        {
            $plist .= $listdd;
        }
        if(preg_match("#next#i", $atts['listitem']))
        {
            $plist .= $nextpage;
        }
        if(preg_match("#end#i", $atts['listitem']))
        {
            $plist .= $endpage;
        }
        if(preg_match("#form#i", $atts['listitem']))
        {
            $plist .=" <form name='pagelist' action='".$this->GetCurUrl()."' style='float:left;' class='pagelistform'>$hidenform";
            if($totalpage>$total_list)
            {
                $plist.="<input type='text' name='pageno' style='padding:0px;width:30px;height:18px;font-size:11px' />\r\n";
                $plist.="<input type='submit' name='plistgo' value='GO' style='padding:0px;width:30px;height:22px;font-size:11px' />\r\n";
            }
            $plist .= "</form>\n";
        }
        $plist .= "<!--分页结束-->\n";
        return $plist;
    }
    //获得当前网址
    function GetCurUrl()
    {
        if(!empty($_SERVER["REQUEST_URI"]))
        {
            $nowurl = $_SERVER["REQUEST_URI"];
            $nowurls = explode("?",$nowurl);
            $nowurl = $nowurls[0];
        }
        else
        {
            $nowurl = $_SERVER["PHP_SELF"];
        }
       		return $nowurl;		
		
    }
	
	//数据库中读取 一条记录
	function dbRow($sql){
		$row = dbRow($sql);
		return $row;
	}
	//数据库中读取 多条记录
	function dbList($sql){
		$list = dbList($sql);
		return $list;
	
	}
	
    //关闭
    function Close()
    {

    }


	
	
	

	
	
	
}//end class ListPage

/*********************************************************/

/** 
 *  获取执行时间 
 *  例如:$t1 = ExecTime(); 
 *       在一段内容处理之后: 
 *       $t2 = ExecTime(); 
 *  我们可以将2个时间的差值输出:echo $t2-$t1; 
 * 
 *  @return    int 
 */  
if ( ! function_exists('ExecTime'))  
{  
    function ExecTime()  
    {  
        $time = explode(" ", microtime());  
        $usec = (double)$time[0];  
        $sec = (double)$time[1];  
        return $sec + $usec;  
    }  
}  

